package com.kefu.robot.controller;

import com.kefu.common.vo.DetailVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kefu.common.annotation.Permission;
import com.kefu.common.controller.AbstractBaseController;
import com.kefu.common.exception.BusinessException;
import com.kefu.common.result.Result;
import com.kefu.common.result.ResultGenerator;
import com.kefu.common.vo.DeleteVo;
import com.kefu.robot.model.RobotDocNode;
import com.kefu.robot.model.RobotDocNodeItem;
import com.kefu.robot.service.RobotDocNodeItemService;
import com.kefu.robot.service.RobotDocNodeService;
import com.kefu.robot.vo.RobotDocNodeInsertVo;
import com.kefu.robot.vo.RobotDocNodeItemInsertVo;
import com.kefu.robot.vo.RobotDocNodeItemUpdateVo;
import com.kefu.robot.vo.RobotDocNodeSearchVo;
import com.kefu.robot.vo.RobotDocNodeUpdateVo;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * @author CodeGenerator
 * @date 2021/10/10
 */
@Api(tags = "多轮问题节点")
@RestController
@RequestMapping("robot/docnode")
public class RobotDocNodeController extends AbstractBaseController {

    @Autowired
    private RobotDocNodeService robotDocNodeService;
    @Autowired
    RobotDocNodeItemService robotDocNodeItemService;

    @PostMapping("/add")
    @ApiOperation(value = "新增多轮问题节点", notes = "新增多轮问题节点")
    public Result<String> add(@Validated @RequestBody RobotDocNodeInsertVo robotDocNodeVo) {
    	logger.info("新增多轮问题节点:{}", robotDocNodeVo);
        if (robotDocNodeVo.getIsFirst()==1 && robotDocNodeService.count(Wrappers.lambdaQuery(RobotDocNode.class)
                .eq(RobotDocNode::getDocId, robotDocNodeVo.getDocId())
                .eq(RobotDocNode::getIsFirst, 1)) > 0) {
            throw BusinessException.msg("开始节点已经存在");
        }
    	RobotDocNode info = robotDocNodeVo.convert();
        robotDocNodeService.save(info);
        return ResultGenerator.genSuccessResult(info.getNodeId());
    }

    @PostMapping("/delete")
    @ApiOperation(value = "删除多轮问题节点", notes = "删除多轮问题节点")
    public Result<String> delete(@Validated @RequestBody DeleteVo delete) {
    	logger.info("删除多轮问题节点:{}", delete);
        String nodeId = delete.getId();
        if (robotDocNodeService.getById(nodeId).getIsFirst() == 1) {
            throw BusinessException.msg("开始节点不能删除");
        }
        if (robotDocNodeItemService.count(Wrappers.lambdaQuery(RobotDocNodeItem.class)
                .eq(RobotDocNodeItem::getNextNodeId, nodeId)) > 0) {
            throw BusinessException.msg("节点已经被关联，暂时无法删除");
        }
        robotDocNodeService.deleteNode(delete.getId());
        return ResultGenerator.genSuccessResult();
    }

    @PostMapping("/update")
    @ApiOperation(value = "修改多轮问题节点", notes = "修改多轮问题节点")
    public Result<String> update(@Validated @RequestBody RobotDocNodeUpdateVo robotDocNodeVo) {
    	logger.info("修改多轮问题节点:{}", robotDocNodeVo);
    	
    	RobotDocNode info = robotDocNodeVo.convert();
        robotDocNodeService.update(info);
        return ResultGenerator.genSuccessResult();
    }

    @PostMapping("/list")
    @ApiOperation(value = "查询多轮问题节点", notes = "查询多轮问题节点")
    public Result<Page<RobotDocNode>> list(@Validated @RequestBody RobotDocNodeSearchVo search) {    
        Page<RobotDocNode> page = robotDocNodeService.search(search);
        for(RobotDocNode node : page.getRecords()){
            node.setItemList(robotDocNodeService.getItemList(node.getNodeId()));
        }
        return ResultGenerator.genSuccessResult(page);
    }


    @PostMapping("/addItem")
    @ApiOperation(value = "新增多轮问题节点Item", notes = "新增多轮问题节点Item")
    public Result<String> addItem(@Validated @RequestBody RobotDocNodeItemInsertVo robotDocNodeItemVo) {
        logger.info("新增多轮问题节点Item:{}", robotDocNodeItemVo);

        RobotDocNodeItem info = robotDocNodeItemVo.convert();
        robotDocNodeService.addItem(info);
        return ResultGenerator.genSuccessResult(info.getItemId());
    }

    @PostMapping("/deleteItem")
    @ApiOperation(value = "删除多轮问题节点Item", notes = "删除多轮问题节点Item")
    public Result<String> deleteItem(@Validated @RequestBody DeleteVo delete) {
        logger.info("删除多轮问题节点Item:{}", delete);

        robotDocNodeService.deleteItem(delete.getId());
        return ResultGenerator.genSuccessResult();
    }

    @PostMapping("/updateItem")
    @ApiOperation(value = "修改多轮问题节点Item", notes = "修改多轮问题节点Item")
    public Result<String> updateItem(@Validated @RequestBody RobotDocNodeItemUpdateVo robotDocNodeItemVo) {
        logger.info("修改多轮问题节点Item:{}", robotDocNodeItemVo);

        RobotDocNodeItem info = robotDocNodeItemVo.convert();
        robotDocNodeService.updateItem(info);
        return ResultGenerator.genSuccessResult();
    }

    @PostMapping("/copyNode")
    @ApiOperation(value = "复制多轮节点", notes = "复制多轮节点")
    public Result<RobotDocNode> copyNode(@Validated @RequestBody DetailVo detailVo) {
        logger.info("复制多轮节点:{}", detailVo);

        RobotDocNode node=robotDocNodeService.copy(detailVo.getId());
        return ResultGenerator.genSuccessResult(node);
    }
}
